package demo05_topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import utils.ConnectionUtil;

import java.io.IOException;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeoutException;

/**
 * @Description Topic 模式下的消息生产者
 * @Author shihan
 * @Date 2020/12/26 10:49
 * @Version 1.0
 */
public class TProducer {

    public static final String EXCHANGE_TOPIC_NAME = "exc.topic.ename";

    public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException, URISyntaxException, IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri(ConnectionUtil.URI);

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        // 声明一个 Topic 类型的交换器
        channel.exchangeDeclare(EXCHANGE_TOPIC_NAME, BuiltinExchangeType.TOPIC);

        // 准备消息，指明 Exchange 和 Routing key，并进行消息发送
        List<String> stringList = Arrays.asList(
                "quick.orange.rabbit",
                "lazy.orange.elephant",
                "quick.orange.fox",
                "lazy.brown.fox",
                "lazy.pink.rabbit",
                "quick.brown.fox",
                "quick.orange.male.rabbit",
                "lazy.orange.male.rabbit");

        for (String msg : stringList) {
            channel.basicPublish(EXCHANGE_TOPIC_NAME,msg,null,msg.getBytes("utf-8"));
        }

        // 生成环境使用池化技术
        channel.close();
        connection.close();

    }

}
